package com.hb.umsp.service;

import com.hb.umsp.entity.*;
import com.hb.umsp.mapper.*;
import com.hb.umsp.util.token.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ExamUserService {

    @Autowired
    TokenUtil tokenUtil;

    @Autowired
    HbExamUserDetailMapper hbExamUserDetailMapper;

    @Autowired
    HbExamDetailMapper hbExamDetailMapper;

    @Autowired
    HbExamMapper hbExamMapper;

    @Autowired
    HbExamHistoryMapper hbExamHistoryMapper;

    @Autowired
    HbExamUserDetailHistoryMapper hbExamUserDetailHistoryMapper;

    @Transactional
    public void createUserExam(List<HbExamUserDetail> hbExamUserDetail,
                               String token) {

        if (hbExamUserDetail == null) {
            return;
        }

        Map<String,String> map = tokenUtil.parseToken(token);
        String username = map.get("userId");
        int score = getScore(hbExamUserDetail);

        for (int i=0; i<hbExamUserDetail.size(); i++) {
            hbExamUserDetail.get(i).setUsername(username);
        }

        HbExam exam = hbExamMapper.selectExamByExamCode(hbExamUserDetail.get(0).getExamcode());
        HbExamHistory hbExamHistory = new HbExamHistory();

        if(exam != null ) {
            hbExamHistory.setExamcode(exam.getExamCode());
            hbExamHistory.setExamname(exam.getExamName());
            hbExamHistory.setDepcode(exam.getDepcode());
            hbExamHistory.setScore(exam.getScore());
            hbExamHistory.setExamCount(exam.getExamCount());
            hbExamHistory.setIsfinish(exam.getIsFinish());
            hbExamHistory.setIspublic(exam.getIspublic());
            hbExamHistory.setOperator(exam.getOperator());
            hbExamHistory.setRemark(exam.getRemark());
        }
        List<HbExamUserDetailHistory> hbExamUserDetailHistories = new ArrayList<>();
        if (exam.getExamCount() != 0 ) { //如果没有做过
            List<HbExamUserDetail> userDetail = hbExamUserDetailMapper.selectUserDetailByExamCode(exam.getExamCode());
            HbExamUserDetailHistory hbExamUserDetailHistory = new HbExamUserDetailHistory();
            for (int i=0; i<userDetail.size(); i++) {
                hbExamUserDetailHistory.setExamcode(userDetail.get(i).getExamcode());
                hbExamUserDetailHistory.setExamCount(exam.getExamCount());
                hbExamUserDetailHistory.setUsername(userDetail.get(i).getUsername());
                hbExamUserDetailHistory.setAnscode(userDetail.get(i).getAnscode());
                hbExamUserDetailHistory.setQuestioncode(userDetail.get(i).getQuestioncode());
                hbExamUserDetailHistories.add(hbExamUserDetailHistory);
            }
        }

        HbExam hbExam = new HbExam();
        hbExam.setScore(score);
        hbExam.setExamCount(hbExam.getExamCount()+1);  //考试次数+1
        hbExam.setExamCode(hbExamUserDetail.get(0).getExamcode());

        //插入历史记录
        hbExamHistoryMapper.insert(hbExamHistory);
        hbExamUserDetailHistoryMapper.insert(hbExamUserDetailHistories);
        //删除上一次的答案
        hbExamUserDetailMapper.deleteByExamCode(hbExam.getExamCode());
        //插入新答案
        hbExamUserDetailMapper.insert(hbExamUserDetail);
        //更新考试信息
        hbExamMapper.updateExam(hbExam);

    }

    public int getScore(List<HbExamUserDetail> hbExamUserDetail) {

        if (hbExamUserDetail.size() < 0) {
            return 0;
        }

        List<HbExamDetail> list = hbExamDetailMapper.selectExamDetailByExamCode(
                hbExamUserDetail.get(0).getExamcode());

        int score = 0;
        for (int i=0; i<list.size(); i++) {

            HbExamDetail hbExamDetail = list.get(i);
            if (hbExamDetail.getQuestionType().contains("04")) { //跳过简答题
                continue;
            }
            List<HbQuestionAns> hbQuestionAns = hbExamDetail.getQuestionAnsList();

            HbExamUserDetail userDetail = new HbExamUserDetail();
            int index = 0;
            for (int j=0; i<hbExamUserDetail.size(); j++) {
                userDetail = hbExamUserDetail.get(j);
                for (int m=0; m<hbQuestionAns.size(); m++) {
                    if (hbQuestionAns.get(m).getQuestionCode().
                            equals(userDetail.getQuestioncode())) {
                        index ++;
                    }
                }
            }
            if (index == hbQuestionAns.size()) {
                score += hbExamDetail.getQuestionScore();
            }
        }

        return score;
    }
}
